-- 代码变更时实时诊断（通过 nvim-lint）
return {
  "mfussenegger/nvim-lint",
  event = "BufReadPre",
  -- event = { "BufWritePost", "BufReadPost" },
  config = function()
    require("lint").linters_by_ft = {
      -- lua = { "selene", "luacheck" },
      lua = { "luacheck" },
      python = { "flake8", "mypy", "pylint" },

      javascript = { "eslint_d" },
      typescript = { "eslint_d" },
      javascriptreact = { "eslint_d" },
      typescriptreact = { "eslint_d" },
      json = { "jsonlint" },

      yaml = { "yamllint" },
      markdown = { "markdownlint" },
      -- sh = { "shellcheck" },
      -- go = { "staticcheck", "golangci-lint" }, -- 报错
      -- rust = { "clippy" },
      -- c = { "cppcheck" },
      -- cpp = { "cppcheck" },
      ["*"] = { "codespell" }, -- 所有文件的拼写检查
    }

    -- 限制诊断频率
    vim.api.nvim_create_autocmd(
      { "BufWritePost", "InsertLeave", "BufReadPost" },
      {
        pattern = "*",
        callback = function()
          vim.defer_fn(function()
            require("lint").try_lint()
            require("lint").try_lint("codespell")
          end, 2000) -- 延迟2秒
        end,
      }
    )

    -- -- 自定义 linter 配置
    -- require("lint").linters = {
    --   eslint_d = {
    --     cmd = "eslint_d",
    --     args = {
    --       "--stdin",
    --       "--stdin-filename",
    --       "$FILENAME",
    --       "--format",
    --       "json",
    --     },
    --     stdin = true,
    --     parser = function(output)
    --       local decoded = vim.json.decode(output)
    --       return decoded[1].messages
    --     end,
    --   },
    --   clippy = {
    --     cmd = "cargo",
    --     args = { "clippy", "--message-format=json" },
    --     stdin = false,
    --     parser = function(output)
    --       local decoded = vim.json.decode(output)
    --       return decoded[1].messages
    --     end,
    --   },
    -- }

    -- -- 自动触发诊断
    -- vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost" }, {
    --   callback = function()
    --     require("lint").try_lint()
    --   end,
    -- })
  end,
}
